package com.inool.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.inool.service.UserService;
import com.inool.vo.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inool.common.jwt.JwtUtil;
import com.inool.common.utils.MD5;
import com.inool.model.Student;
import com.inool.model.Teacher;
import com.inool.model.User;
import com.inool.mapper.StudentMapper;
import com.inool.mapper.TeacherMapper;
import com.inool.mapper.UserMapper;
import com.inool.common.exception.InoolException;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Map<String, Object> login(LoginVo loginVo) {
        Map<String,Object> map =new HashMap<>();
    
                //获取输入的用户名和密码
                User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserId, loginVo.getUserId()));
                if(user==null){
                    //是老师
                    if(loginVo.getUserType().equals("teacher")){ 

                        Teacher teacher =  teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getStaffNumber,loginVo.getUserId()));
                        if(teacher==null){
                            throw new InoolException(201,"老师不存在");
                        }else{ 
                            user = new User(teacher.getStaffNumber(),teacher.getName(),MD5.encrypt(loginVo.getPassword()),"TEACH");
                            save(user);
                        }
                        
                    }else if(loginVo.getUserType().equals("student")){ //是学生
                        Student student =  studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStudentNumber,loginVo.getUserId()));
                        if(student==null){
                            throw new InoolException(201,"学生不存在");
                        }else{ 
                            user = new User(student.getStudentNumber(),student.getName(),MD5.encrypt(loginVo.getPassword()),"STU");
                            save(user);
                        } 
                    }else{
                        throw new InoolException(201,"用户类型错误");
                    }
                   
                }
                //判断密码
                if(!user.getPassword().equals(MD5.encrypt(loginVo.getPassword()))){
                    throw new InoolException(201,"密码错误");
                }
                //使用JWt根据用户id联合名称生成token
                map.put("token",JwtUtil.createToken(user.getUserId(), user.getUsername(), user.getRole()));
                //返回
       
        return map;
    }
    @Override
    public IPage<User> pageUsers(IPage<User> page) {
   
        return page(page);
    }
    
}